package edu.ustb.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/WebContent/*")
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {
    }
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //System.out.println("Encoding 过滤。。。。");
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        /*HttpServletResponse servletResponse = (HttpServletResponse) response;*/
        //只过滤get请求
        if("POST".equalsIgnoreCase(servletRequest.getMethod()))
        {
            servletRequest.setCharacterEncoding("utf-8");
        }
        /*servletResponse.setContentType("text/plain;charset=utf-8");
        servletResponse.setCharacterEncoding("utf-8");*/
        chain.doFilter(servletRequest,response);
    }
}
